package com.mhc.suzuki.mq;

import com.camaro.starter.mq.annotation.MQConsumer;
import com.camaro.starter.mq.base.AbstractMQPushConsumer;
import com.mhc.fiat.api.ProcessServiceWraper;
import com.mhc.fiat.api.domain.ProcessEventType;
import com.mhc.fiat.api.message.ProcessMessage;
import com.mhc.suzuki.service.MessageService;
import com.mhc.suzuki.service.impl.OrderAuditTaskServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

/**
 * 流程消息接口
 * Created by HMJ on 2017/9/6.
 */
@Slf4j
@MQConsumer(topic = "fiat.mq.topic", consumerGroup = "fiat.mq.consumerGroup", tag="ProcessMessage_3")
public class MqMessageService extends AbstractMQPushConsumer<ProcessMessage> {

    @Autowired
    private OrderAuditTaskServiceImpl orderAuditTaskServiceImpl;

    @Autowired
    private MessageService messageService;

//    @Autowired
//    private ProcessServiceWraper processServiceWraper;


    @Override
    public boolean processWithKey(String s, ProcessMessage processMessage) {
        log.info("获取返回消息:{}", processMessage);
        Map<String, Object> msg = processMessage.getProcessVariables();

        try {
            if(processMessage.getProcessEventType().equals(ProcessEventType.TASK_CREATED)){
                /**当processEventType==TASK_CREATED，生成待办任务**/
                orderAuditTaskServiceImpl.createAuditTask(processMessage,msg);
            }
            if(processMessage.getProcessEventType().equals(ProcessEventType.PROCESS_COMPLETED)){
                /**当processEventType==PROCESS_COMPLETED，表示流程结束**/
                orderAuditTaskServiceImpl.getAuditTaskList(msg);
                messageService.sendEndMessage(processMessage, Integer.parseInt(processMessage.getProcessVariables().get("applyType").toString()));
            }
            return true;
        }catch (Exception e){
            log.error("待办任务创建失败，原因："+e.getMessage()+ "重新创建中...: " ,e);
            /**根据流程实例编号、执行人找出对应的流程数据**/
            return true;
        }
    }


}
